package leetcode;

public class Leetcode331 {
    public boolean isValidSerialization(String preorder) {
        int num = 0;
        for (int i = preorder.length()-1; i >= 0; i--) {
            if(preorder.charAt(i)==','){
                continue;
            }
            if(preorder.charAt(i)=='#'){
                num++;
            }else{
                while(i>=0 && preorder.charAt(i) != ','){
                    i--;
                }//节点数字可能有多位

                if(num>=2){
                    num--;
                }else{
                    return false;
                }
            }
        }
        if(num==1){
            return true;
        }else{
            return false;
        }
    }
}
